package com.yuanfeng.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.business.dto.ShopDecorationBlockDTO;
import com.yuanfeng.business.entity.ShopDecorationBlockEntity;
import com.yuanfeng.business.mapper.ShopDecorationBlockMapper;
import com.yuanfeng.business.service.ShopDecorationBlockService;
import com.yuanfeng.commoms.aspect.UserPermissionsResult;
import com.yuanfeng.commoms.aspect.UserService;
import com.yuanfeng.commoms.config.PermissionsConstant;
import com.yuanfeng.commoms.dto.userms.UserTokenBeanDTO;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.ResponseResult;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 *
 */
@Service
public class ShopDecorationBlockServiceImpl extends ServiceImpl<ShopDecorationBlockMapper, ShopDecorationBlockEntity>
    implements ShopDecorationBlockService{

    @Override
    public ResponseResult queryShopDecorationBlock(UserTokenBeanDTO param) {

        // 权限判断
        UserPermissionsResult userPermissionsResult = UserService
                .judgePermissions(PermissionsConstant.SERLLER_SHOP_SETSHOP, param.getToken());

        if (userPermissionsResult.getFlag()) {
            List<ShopDecorationBlockEntity> entity = this.baseMapper.selectList(new LambdaQueryWrapper<ShopDecorationBlockEntity>()
                              .eq(ShopDecorationBlockEntity::getShopId,Integer.valueOf(userPermissionsResult.getShopId())).orderByAsc(ShopDecorationBlockEntity::getBlockSort));

            return ResponseResult.success(entity);
        } else {
            return ResponseResult.fail(BizCodeEnume.INSUFFICIENT_PERMISSIONS);
        }

    }

    @Override
    public ResponseResult delShopDecorationBlock(Map<String, Object> param) {
        // 权限判断
        UserPermissionsResult userPermissionsResult = UserService
                .judgePermissions(PermissionsConstant.SERLLER_SHOP_SETSHOP,param.get("token").toString());

        if (userPermissionsResult.getFlag()) {
            this.baseMapper.deleteById(param.get("blockId").toString());
        } else {
            return ResponseResult.fail(BizCodeEnume.INSUFFICIENT_PERMISSIONS);
        }
        return ResponseResult.success(BizCodeEnume.OPERATION_SUCCESSFUL);
    }

    @Override
    public ResponseResult addShopDecorationBlock(ShopDecorationBlockDTO param) {
        // 权限判断
        UserPermissionsResult userPermissionsResult = UserService
                .judgePermissions(PermissionsConstant.SERLLER_SHOP_SETSHOP, param.getToken());

        if (userPermissionsResult.getFlag()) {
            Integer shopId = Integer.valueOf(userPermissionsResult.getShopId());
            param.setShopId(shopId);

            if (null == param.getBlockId()) {
                Integer blockSort = this.baseMapper.selectMaxBlockSort(shopId);

                if (null == blockSort || blockSort.equals("")) {
                    blockSort = 0;
                }
                param.setBlockSort(blockSort + 1);
                this.baseMapper.insertSelective(param);
            } else {
                this.baseMapper.updateByPrimaryKeySelective(param);
            }

        } else {
            return ResponseResult.fail(BizCodeEnume.INSUFFICIENT_PERMISSIONS);
        }
        return ResponseResult.success(BizCodeEnume.OPERATION_SUCCESSFUL);
    }
}




